//
//  ForgetParrWordViewController.m
//  HomeDelivery
//
//  Created by apple on 16/7/25.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "ForgetPassWordViewController.h"
#import "AlterPswViewController.h"
@interface ForgetPassWordViewController ()
{
    NSTimer *_timer;
    NSInteger _second;
}
@end

@implementation ForgetPassWordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"重置登录密码";
    self.getCodeBtn.layer.cornerRadius=5;
    self.getCodeBtn.clipsToBounds=YES;
    self.nextBtn.layer.cornerRadius=5;
    self.nextBtn.clipsToBounds=YES;
    self.view.backgroundColor=LIGHTGRAY;
    _timer=[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(changeText) userInfo:nil repeats:YES];
    [_timer setFireDate:[NSDate distantFuture]];
}
-(void)changeText
{
    if (_second==0)
    {
        [_timer setFireDate:[NSDate distantFuture]];
        [_getCodeBtn setTitle:@"重新发送" forState:UIControlStateNormal];
    }else
    {
        [_getCodeBtn setTitle:[NSString stringWithFormat:@"%zd秒",_second] forState:UIControlStateNormal];
    }
    _second--;
}


-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


- (IBAction)getCode:(id)sender
{
    [self.view endEditing:YES];
    UIButton *btn=(UIButton *)sender;
    if ([[btn titleForState:UIControlStateNormal]hasSuffix:@"秒"]) {
        return;
    }

    if (self.phoneTx.text.length==0) {
        [MBProgressHUD showError:@"请输入手机号"];
        [self.phoneTx becomeFirstResponder];
    }else if ([GeneralTool valiMobile:self.phoneTx.text])
    {
        [MBProgressHUD showError:[GeneralTool valiMobile:self.phoneTx.text]];
        [self.phoneTx becomeFirstResponder];

    }else
    {
        //http://192.168.1.107:3722/User/VerifyCode
        //m_VerifyCodePara(Mobile,ClientType,Timestamp,Sign)
        NSString *timeStamp=[GeneralTool gettimestamp];
        [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        NSDictionary *dic=@{@"Mobile":self.phoneTx.text,
                            @"ClientType":CLIENTTYPE,
                            @"Timestamp":timeStamp};
        NSString *sign=[GeneralTool GetSin:dic];
        NSDictionary *dataDic=@{@"Mobile":self.phoneTx.text,
                                @"ClientType":CLIENTTYPE,
                                @"Timestamp":timeStamp,
                                @"Sign":sign};
        NSDictionary *requestDic=@{@"m_VerifyCodePara":dataDic};
        [RequestTool requestDataWithType:@"post" andUrlString:@"User/VerifyCode" andParameters:requestDic andFinishedWith:^(id response, NSError *error) {
            [MBProgressHUD hideHUDForView:self.view];
            if (error)
            {
                [MBProgressHUD showError:@"请求失败"];
            }else
            {
                if ([response[@"IsSuccess"]integerValue]==1)
                {
                    _second=60;
                    [_timer setFireDate:[NSDate distantPast]];
                    [MBProgressHUD showSuccess:response[@"ReturnString"]];
                }else
                {
                    [MBProgressHUD showError:response[@"ReturnString"]];
                    
                }
            }
            
            
        }];
    }

    
}
- (IBAction)goToNextStep:(id)sender
{
    if (self.phoneTx.text.length==0) {
        [MBProgressHUD showError:@"请输入手机号"];
    }else if ([GeneralTool valiMobile:self.phoneTx.text])
    {
        [MBProgressHUD showError:[GeneralTool valiMobile:self.phoneTx.text]];
    }else if(self.codeTx.text.length==0)
    {
        [MBProgressHUD showError:@"请输入验证码"];
    }else
    {
        [self checkCode];
    }
    
}
-(void)checkCode
{
    //http://192.168.1.107:32807/User/CheckVerifyCode
    //m_CheckVerifyCodePara(Mobile,Code,ClientType,Timestamp,Sign)
    NSString *timeStamp=[GeneralTool gettimestamp];
    NSDictionary *dic=@{@"Mobile":self.phoneTx.text,
                        @"Code":self.codeTx.text,
                        @"ClientType":CLIENTTYPE,
                        @"Timestamp":timeStamp};
    NSString *sign=[GeneralTool GetSin:dic];
    NSDictionary *dataDic=@{@"Mobile":self.phoneTx.text,
                            @"Code":self.codeTx.text,
                            @"ClientType":CLIENTTYPE,
                            @"Timestamp":timeStamp,
                            @"Sign":sign};
    NSDictionary *requestDic=@{@"m_CheckVerifyCodePara":dataDic};
    [RequestTool requestDataWithType:@"post" andUrlString:@"User/CheckVerifyCode" andParameters:requestDic andFinishedWith:^(id response, NSError *error) {
        if (error)
        {
            [MBProgressHUD showError:@"请求失败"];
        }else
        {
            if ([response[@"IsSuccess"]integerValue]==1)
            {
                AlterPswViewController *alterPsw=[[AlterPswViewController alloc]init];
                alterPsw.verifyCode=self.codeTx.text;
                alterPsw.mobile=self.phoneTx.text;
                [self.navigationController pushViewController:alterPsw animated:YES];
            }else
            {
                [MBProgressHUD showError:response[@"ReturnString"]];

            }
        }
    }];
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
      self.navigationController.navigationBar.barTintColor=[UIColor whiteColor];
     self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor blackColor], NSFontAttributeName:[UIFont systemFontOfSize:18]};
    
}

@end
